<template>
	<el-dialog v-model="dialogShow" title="充值" width="600" :before-close="cancel">
		<!-- <template v-if="dialogShow">
			<EDescriptions class="mb-2" :column="2" :formOptions="detailOptions" :formData="formData" />
			<FlowInfo ref="flowInfoRef" :processedInstanceId="formData.processedInstanceId" />
		</template> -->
		<EForm
			v-if="dialogShow"
			class="mr-10px"
			ref="dialogForm"
			:form-options="editOptions"
			form-label-width="120px"
			:form-rules="formRules"
			:form-data-current="dialogFormCurrent"
		>
		</EForm>
		<template #footer>
			<div class="dialog-footer">
				<el-button type="primary" @click="submit">确认充值</el-button>
				<el-button @click="cancel">取 消</el-button>
			</div>
		</template>
	</el-dialog>
</template>
<script setup>
import { editFormOptions, formRules } from './config';
import { ElMessage } from 'element-plus';
import EForm from '@/components/e-form';
import { useDict } from '/@/hooks/dict';
import { useUserInfo } from '/@/stores/userInfo';
import { accountDetail, recharge } from '/@/api/insurance';

const dialogShow = ref(false);
const dialogForm = ref(null);
const dialogFormCurrent = ref();

const { insurance_company } = useDict('insurance_company');

const editOptions = reactive(editFormOptions);

const { userInfos } = useUserInfo();
const loadData = async () => {
	editOptions.forEach((item) => {
		if (item.key == 'companyId') {
			item.options = insurance_company;
		} 
		// else if (item.key == 'categoryId') {
		// 	item.options = budget_type;
		// }
	});
};
loadData();


const show = async () => {
	const { data } = await accountDetail({
		userId: userInfos.user.userId
	})
	if (!data) {
		return
	}
	dialogFormCurrent.value = {
		balance: `${data.balance}`,
	};
	dialogShow.value = true;
};
const closeDialog = () => {
	dialogShow.value = false;
};

const cancel = () => {
	closeDialog();
};

const submit = () => {
	dialogForm.value.handleValidate(async (valid) => {
		if (valid) {
			const { companyId, amount } = dialogForm.value.formData
			console.log(dialogForm.value.formData);
			
			const { data: { payUrl } } = await recharge({
				companyId, amount
			});
			if (!payUrl) {
				ElMessage.error('未获取到支付链接，请重新尝试');
				return
			}
			window.location.href = payUrl
		}
	});	
}

defineExpose({
	show,
});
</script>
